﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogic;
using Utility;
using BusinessObject;
namespace WebTeamplate
{
    public partial class Admin_SubCategoryDetail : BasePage
    {
        private string validateGroup = "cateDetail";
        public int SubCategoryID
        {

            get
            {

                object o = ViewState["SubCategoryID"];

                if (o != null) return (int)o;

                return int.MinValue;

                //a default

            }

            set { ViewState["SubCategoryID"] = value; }

        }
        protected void Page_Load(object sender, EventArgs e)
        {
            ClientDefaultButton.SetPageDefaultButton(this.Page, btnSave.ClientID);
            LoadControl();
            if (!Page.IsPostBack)
            {

                InitTextControl();
                CommonFunction.LoadCategory(drlCategory, string.Empty, true);
                InitParamater();
                LoadData();
            }
        }

        private void LoadData()
        {
            SubCategory objData = SubCategoryUtil.Get(this.SubCategoryID);
            if (objData != null)
            {
                if (objData.Published == CommonFunction.True)
                    chkIsActive.Checked = true;
                drlCategory.SelectedValue = objData.CategoryId.ToString();              
                string name = string.Empty;
                foreach (Control ctr in displayContent.Controls)
                {
                    try
                    {
                        name = string.Empty;
                        Admin_usercontrol_ucSubCategoryTrans ctrContent = (Admin_usercontrol_ucSubCategoryTrans)ctr;
                        SetValueByLanguage(ctrContent.languabeCode, ref name, objData.SubCategory_TransCollection);
                        ctrContent.NameValue = name;
                    }
                    catch
                    {
                    }
                }

            }
        }
        public void SetValueByLanguage(string language, ref string name, List<SubCategory_Trans> lsrSource)
        {
            foreach (SubCategory_Trans objTran in lsrSource)
            {
                if (objTran.LanguageId.Trim() == language.Trim())
                {
                    name = objTran.SubCategoryName;
                    break;
                }
            }
        }
        private void InitParamater()
        {
            this.SubCategoryID = ConvertValueObject.ParseInt(CommonFunction.GetValueFromQueryString(this.Page, "subcategoryId"));
            drlCategory.SelectedValue = CommonFunction.GetValueFromQueryString(this.Page, "F_drlCategory");
            this.Command = CommonFunction.GetValueFromQueryString(this.Page, "act");
        }
        private void InitTextControl()
        {
            //  imgBirthDay.Attributes.Add("onclick", "displayCalendar('../calendar/images/',document.forms[0]." + txtBirthDay.ClientID + ",'" + BaseFunctions.DEFAULT_DATE_FORMAT + "',this)");

            btnSave.Text = Resources.Resource.LABEL_BUTTON_SAVE;
            btnCancel.Text = Resources.Resource.LABEL_BUTTON_CANCEL;
            btnReset.Text = Resources.Resource.LABEL_BUTTON_RESET;
            refvCategory.ErrorMessage = string.Format(Resources.Resource.MESSAGE_REQUIRE_INPUT, CommonFunction.GetTextLabel(lblCategory));
            refvCategory.ValidationGroup = validateGroup;

            vsumCategory.ValidationGroup = validateGroup;
            btnSave.ValidationGroup = validateGroup;
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                SubCategory objData = new SubCategory();
                objData.Published = chkIsActive.Checked ? CommonFunction.True : CommonFunction.False;
                objData.CategoryId = ConvertValueObject.ParseInt(drlCategory.SelectedValue);
                objData.SubCategoryCode = string.Empty;
                objData.SubCategory_TransCollection = GetSubCategoryTrans();
                if (this.Command == CommonFunction.EdityCommand)
                    Update(objData);
                else
                    Insert(objData);

            }
            else
            {
                lblErrorMessage.Text = "Data is not valid. Please try again.";
            }
        }

        private void Insert(SubCategory objData)
        {
            if (SubCategoryUtil.Insert(objData))
            {
                Response.Redirect("SubCategory.aspx?" + GetPageParams(FilterFieldType.All));
            }
            else
            {
                lblErrorMessage.Text = Resources.Resource.MESSAGE_INSERT_ERROR;
            }
        }

        private void Update(SubCategory objData)
        {
            objData.SubCategoryId = this.SubCategoryID;
            if (SubCategoryUtil.Update(objData))
            {
                Response.Redirect("SubCategory.aspx?" + GetPageParams(FilterFieldType.All));
            }
            else
            {
                lblErrorMessage.Text = Resources.Resource.MESSAGE_UPDATE_ERROR;
            }
        }
        private List<SubCategory_Trans> GetSubCategoryTrans()
        {
            string name = string.Empty;
            List<SubCategory_Trans> lsrResult = new List<SubCategory_Trans>();
            string[] arrLanguage = hidLanguage.Value.Split('|');
            for (int i = 0; i < arrLanguage.Length; i++)
            {
                if (arrLanguage[i].ToString() != string.Empty)
                {
                    SubCategory_Trans objTran = new SubCategory_Trans();
                    objTran.CreatedBy = this.userLoginName;
                    objTran.CreatedDate = CommonFunction.Now;
                    objTran.LastUpdatedBy = this.userLoginName;
                    objTran.LastUpdatedDate = objTran.CreatedDate;
                    objTran.LanguageId = arrLanguage[i].ToString();
                    objTran.SubCategoryName = GetValueControlByLanguage(objTran.LanguageId);
                    objTran.SubCategoryId = this.SubCategoryID;
                    lsrResult.Add(objTran);
                }
            }
            return lsrResult;
        }
        public string GetValueControlByLanguage(string language)
        {
            foreach (Control ctr in displayContent.Controls)
            {
                try
                {
                    Admin_usercontrol_ucSubCategoryTrans ctrContent = (Admin_usercontrol_ucSubCategoryTrans)ctr;
                    if (ctrContent.languabeCode.Trim() == language.Trim())
                        return ctrContent.NameValue;
                }
                catch
                {
                }
            }
            return string.Empty;
        }
        protected void btnReset_Click(object sender, EventArgs e)
        {
            Response.Redirect(this.Request.RawUrl);
        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("SubCategory.aspx?" + GetPageParams(FilterFieldType.All));
        }
        public string Command
        {

            get
            {
                object o = ViewState["Command"];
                if (o != null) return (string)o;
                return string.Empty;
            }

            set { ViewState["Command"] = value; }

        }
        public string ParaLanguageId
        {

            get
            {
                object o = ViewState["ParaLanguageId"];
                if (o != null) return (string)o;
                return string.Empty;
            }

            set { ViewState["ParaLanguageId"] = value; }

        }
        private void LoadControl()
        {
            int index = 0;
            hidLanguage.Value = string.Empty;
            string tabHeader = string.Empty;
            List<Language> lstLanguage = LanguageUtil.GetAll();
            if (lstLanguage != null && lstLanguage.Count > 0)
            {
                foreach (Language objLanguage in lstLanguage)
                {
                    if (objLanguage.Published == CommonFunction.True)
                    {
                        hidLanguage.Value += objLanguage.LanguageId + "|";
                        tabHeader += "<li><a rel='ct" + objLanguage.LanguageId + "_" + index.ToString() + "'><span>" + objLanguage.Name + "</span></a></li>\n";
                        //Admin_UserControl_ucMultilanguageFormFAQ
                        UserControl ctrl = new UserControl();
                        Admin_usercontrol_ucSubCategoryTrans ct = (Admin_usercontrol_ucSubCategoryTrans)ctrl.LoadControl("~/Admin/usercontrol/ucSubCategoryTrans.ascx");
                        ct.id = "ct" + objLanguage.LanguageId + "_" + index.ToString();
                        ct.languageName = objLanguage.Name;
                        ct.languabeCode = objLanguage.LanguageId;
                        ct.ValidationGroup = validateGroup;
                        ct.TextLabelName = ct.TextLabelName + "( " + objLanguage.Name + ")";
                        displayContent.Controls.Add(ct);
                        if (objLanguage.LanguageId.Trim() == this.ParaLanguageId)
                            hidCurrentTagLanguage.Value = index.ToString();
                        //if (objLanguage.LanguageId.Trim() == CommonFunction.DEFAULT_ADMIN_LANGUAGE)
                        if (CommonFunction.CheckRequireData(objLanguage.LanguageId))

                            ct.RequireName = true;
                        else
                            ct.RequireName = false;

                        index++;
                    }
                }
            }
            ulLanguage.InnerHtml = tabHeader;
            this.Page.RegisterStartupScript("DefaultTab", "<script>ddtabmenu.definemenu(\"ddtabs4\", " + hidCurrentTagLanguage.Value.Trim() + ");</script>");
            // lblScript.Text = "<script>ddtabmenu.definemenu(\"ddtabs4\", " + hidCurrentTagLanguage.Value.Trim() + ");</script>";
        }
    }
}
